﻿using System;
using System.Security.Claims;
using ASEPMainProgram.Entities.MainFrame;
using Microsoft.AspNetCore.Http;
using XinjeMesProgram.Entities.User;

namespace MesAPI.Extensions.AuthContext
{
    /// <summary>
    /// 
    /// </summary>
    public static class AuthContextService
    {
        private static IHttpContextAccessor _context;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="httpContextAccessor"></param>
        public static void Configure(IHttpContextAccessor httpContextAccessor)
        {
            _context = httpContextAccessor;
        }
        /// <summary>
        /// 
        /// </summary>
        public static HttpContext Current => _context.HttpContext;
        /// <summary>
        /// 
        /// </summary>
        public static AuthContextUser CurrentUser
        {
            get
            {
                var user = new AuthContextUser()
                {
                    UserName = Current.User.FindFirstValue("UserName"),
                    UserType = (UserType)Convert.ToInt32(Current.User.FindFirstValue("userType")),
                    // Avator = Current.User.FindFirstValue("avator"),
                    UserID = Current.User.FindFirstValue("UserId"),
                    RegGroupId = Current.User.FindFirstValue("RegGroupId"),
                    //UserContact = Current.User.FindFirstValue("Phone"),
                    RoleCode = Current.User.FindFirstValue("RoleCode"),
                    DepId = Current.User.FindFirstValue("DepId"),
                    DepName = Current.User.FindFirstValue("DepName"),
                    GroupType = Current.User.FindFirstValue("GroupType"),
                    ProductGroup = Current.User.FindFirstValue("ProductGroup"),
                    //RootTreeId = Current.User.FindFirstValue("RootTreeId"),
                    // CustomerType = (CustomerType)Convert.ToInt32(Current.User.FindFirstValue("CustomerType")),
                    //DeptId = Current.User.FindFirstValue("DeptName"),
                };
                return user;
            }
        }

        /// <summary>
        /// 是否已授权.
        /// </summary>
        public static bool IsAuthenticated
        {
            get
            {
                return Current.User.Identity.IsAuthenticated;
            }
        }

        /// <summary>
        /// 是否是超级管理员.
        /// </summary>
        public static bool IsSupperAdministator
        {
            get
            {
                return ((UserType)Convert.ToInt32(Current.User.FindFirstValue("userType"))== UserType.SuperAdministator);
            }
        }
    }
}
